//
//  KeyboardAwareModifer.swift
//  JiaoZiTaxi
//
//  Created by Hu Gang on 2023/12/8.
//

import Foundation
import SwiftUI

struct KeyboardAwareModifier: ViewModifier {
   @State private var keyboardHeight: CGFloat = 0

   func body(content: Content) -> some View {
      content
         .padding(.bottom, keyboardHeight)
         .onAppear {
            NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: .main) { notification in
               if let keyboardSize = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect {
                  keyboardHeight = keyboardSize.height
               }
            }
            NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: .main) { _ in
               keyboardHeight = 0
            }
         }
   }
}
